<?php

namespace app\modules\clinical\controllers;

use Yii;
use app\modules\clinical\models\Patient;
use app\models\Helpers;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\AccessControl;
use yii\web\Response;
use app\components\PtAccessControl;

/**
 * PatientInfoController implements the CRUD actions for Patient model.
 */
class PatientController extends Controller
{
    public $enableCsrfValidation = false;

    public function behaviors()
    {
        return [
            // 'access' => [
            //     'class' => AccessControl::className(),
            //     'rules' => [
            //         [
            //             'allow' => true,
            //             'roles' => ['@'],
            //         ],
            //     ],
            // ],
            'pt_access' => [
                'class' => PtAccessControl::className(),
                'rules' => [
                    [
                        'allow' => true,
                        'actions' => 'create',
                    ]
                ],
            ]
        ];
    }
    
    /**
     * Lists all Patient models.
     * @return string
     * @throws \yii\web\NotFoundHttpException
     * @author sally
     * @date   2019-05-19
     */
    public function actionIndex()
    {
        $id = Yii::$app->request->get('id', Yii::$app->session->get('pt_id'));

        return $this->render('view', [
            'model' => $this->findModel($id),
        ]);
    }

    /**
     * Creates a new Patient model.
     * If creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $model = new Patient();

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            return $this->redirect('/search/search/patient');
        } else {
            return $this->redirect('/search/search/create-patient');
        }
    }

    /**
     * Updates an existing Patient model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param $id
     *
     * @return string|\yii\web\Response
     * @throws \yii\web\NotFoundHttpException
     * @author sally
     * @date   2019-05-19
     */
    public function actionUpdate($id)
    {
        $model = $this->findModel($id);

        if ($model->load(Yii::$app->request->post()) && $model->save()) {
            $session = Yii::$app->session;
            $session->set('pt_name', $model->pt_name);
            return $this->redirect(['index', 'id' => $model->pk_id]);
        }

        $pt_history_str = $model->clearCheckBox();

        return $this->render('update', [
            'model' => $model,
            'pt_history_str' => $pt_history_str,
        ]);
    }

    /**
     * Deletes an existing Patient model.
     * If deletion is successful, the browser will be redirected to the 'index' page.
     * @param $id
     *
     * @return \yii\web\Response
     * @throws \yii\web\NotFoundHttpException
     * @author sally
     * @date   2019-05-19
     */
    public function actionDelete($id)
    {
        $model = $this->findModel($id);
        $model->delete_at = strtotime(date("Y-m-d H:i:s"));
        if ($model->save()) {
            $session = Yii::$app->session;
            $session->remove('pt_id');
            $session->remove('pt_name');
            $session->remove('dna');
            return $this->redirect('/search');
        }

        return $this->redirect('/patient/'.$id);
    }

    public function actionRepeat()
    {
        Yii::$app->response->format = Response::FORMAT_JSON;
        $name = Yii::$app->request->post('name', false);
        if ($name) {
            $patients = Patient::find()->select(['pk_id', 'pt_name', 'pt_birth'])->where(['like', 'pt_name', $name])->asArray()->all();
            return $patients;
        }
    }

    /**
     * Finds the Patient model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return Patient the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = Patient::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }
}
